package com.world.tg;

import com.world.tg.shiro.MyRealm;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.config.DefaultShiroFilterChainDefinition;
import org.apache.shiro.spring.web.config.ShiroFilterChainDefinition;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.LinkedHashMap;

@SpringBootApplication
public class TgApplication implements WebMvcConfigurer {

    public static void main(String[] args) {
        SpringApplication.run(TgApplication.class, args);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/photo/*").addResourceLocations("file:D:/photo/");
    }

    @Bean
    public Realm getRealm(){
        return new MyRealm();
    }

    // 定义认证过滤规则
    @Bean
    public ShiroFilterChainDefinition shiroFilterChainDefinition(){
        DefaultShiroFilterChainDefinition definition = new DefaultShiroFilterChainDefinition();
        LinkedHashMap<String,String> map = new LinkedHashMap<>();
        // 设置允许匿名访问的路径资源
        map.put("/home/**","anon");
        map.put("/detail/**","anon");
        definition.addPathDefinitions(map);
        return definition;
    }
}
